package fr.enseirb.mycub.structures;

import java.util.ArrayList;

import android.database.Cursor;
import android.provider.BaseColumns;
import android.util.Log;

import com.google.android.maps.GeoPoint;

import fr.enseirb.mycub.db.BikeDBConstants;
import fr.enseirb.mycub.db.TramwayDBConstants;

public class SpecificLine {

	public int id;
	public String name;
	public Line ligne;
	public GeoPoint[] points;
	public double[] segmentslengthCumulated;
	public double length;
	public double travelTimeInMilliSec;
	public Station debut;
	public Station fin;
	public ArrayList<Station> stations;

	public SpecificLine(){
	}

	public SpecificLine(String name, Line line, GeoPoint[] array){
		this.name = name;
		this.ligne = line;
		points = array;
		length = 0;
		segmentslengthCumulated = new double[points.length];
		segmentslengthCumulated[0] = 0;//Plus pratique pour les boucles dans la crea de tramway
		for(int i = 1; i < points.length; i++){
			/**
			 *  Rayon de la terre en metre
			 */
			double rayonTerre = 6378137;
			// premier point
			double lat1 = points[i-1].getLatitudeE6()/1e6;
			double lng1 = points[i-1].getLongitudeE6()/1e6;
			// deuxieme point
			double lat2 = points[i].getLatitudeE6()/1e6;
			double lng2 = points[i].getLongitudeE6()/1e6;
			//Log.i("Routedist", " "+lat1+" "+lng1+" "+lat2+" "+lng2);
			// calcul des angle en radians
			double rlo1 = Math.toRadians(lng1);
			double rla1 = Math.toRadians(lat1);
			double rlo2 = Math.toRadians(lng2);
			double rla2 = Math.toRadians(lat2);

			double dlo = (rlo2 - rlo1) / 2;
			double dla = (rla2 - rla1) / 2;

			double angle = (Math.sin(dla)*Math.sin(dla)) + Math.cos(rla1)*Math.cos(rla2) * (Math.sin(dlo) * Math.sin(dlo));

			length  += rayonTerre * (2*Math.atan2(Math.sqrt(angle),Math.sqrt(1-angle)));


			/*Math.sqrt(Math.pow((points[i].getLatitudeE6()-points[i-1].getLatitudeE6()),2) +
					Math.pow((points[i].getLongitudeE6()-points[i-1].getLongitudeE6()),2)
					);*/
			segmentslengthCumulated[i] = length;
		}
		//travelTimeInMilliSec = length/(Tramway.AVGSPEED * DrawingThread.FPS);//
		travelTimeInMilliSec = (length / 12)*1000; // on va dire 12ms pour un tram
		Log.i("Route",this.name+" length :"+length+" travel time :"+travelTimeInMilliSec);
	}

	public static ArrayList<SpecificLine> queryRoutes(){
		ArrayList<SpecificLine> liste = new ArrayList<SpecificLine>();
		GeoPoint[] temp = new GeoPoint[6];
		temp[0] = new GeoPoint((int)(44.8002825928889*1e6),(int)(-0.609604147071175*1e6));
		temp[1] = new GeoPoint((int)(44.8006983923928*1e6),(int)(-0.609022981547765*1e6));
		temp[2] = new GeoPoint((int)(44.8008640196207*1e6),(int)(-0.608791304766709*1e6));
		temp[3] = new GeoPoint((int)(44.8009628030638*1e6),(int)(-0.608653374397077*1e6));
		temp[4] = new GeoPoint((int)(44.8030945620703*1e6),(int)(-0.606982354183436*1e6));
		temp[5] = new GeoPoint((int)(44.8035078355891*1e6),(int)(-0.60671048219167*1e6));
		liste.add(new SpecificLine("Doyen Brus--->Francois Bordes", null, temp));
		temp = new GeoPoint[4];
		temp[0] = new GeoPoint((int)(44.8035078355891*1e6),(int)(-0.60671048219167*1e6));
		temp[1] = new GeoPoint((int)(44.8043022213948*1e6),(int)(-0.606186605128692*1e6));
		temp[2] = new GeoPoint((int)(44.8044134211858*1e6),(int)(-0.606100179202744*1e6));
		temp[3] = new GeoPoint((int)(44.8059892371851*1e6),(int)(-0.602276599979411*1e6));
		liste.add(new SpecificLine("Francois Bordes---> Arts & métiers", null, temp));
		temp = new GeoPoint[4];
		temp[0] = new GeoPoint((int)(44.8059892371851*1e6),(int)(-0.602276599979411*1e6));
		temp[1] = new GeoPoint((int)(44.8061438873209*1e6),(int)(-0.601708366382065*1e6));
		temp[2] = new GeoPoint((int)(44.8061651291693*1e6),(int)(-0.601615043353206*1e6));
		temp[3] = new GeoPoint((int)(44.8060190701602*1e6),(int)(-0.597995681629786*1e6));
		liste.add(new SpecificLine("Arts & métiers-->Bethanie", null, temp));
		temp = new GeoPoint[2];
		temp[0] = new GeoPoint((int)(44.8060190701602*1e6),(int)(-0.597995681629786*1e6));
		temp[1] = new GeoPoint((int)(44.8072500673306*1e6),(int)(-0.592588666834272*1e6));
		liste.add(new SpecificLine("Bethanie--->Peixotto", null, temp));
		temp = new GeoPoint[2];
		temp[0] = new GeoPoint((int)(44.8072500673306*1e6),(int)(-0.592588666834272*1e6));
		temp[1] = new GeoPoint((int)(44.8119502868312*1e6),(int)(-0.590930735873974*1e6));
		liste.add(new SpecificLine("Peixotto--->Forum", null, temp));
		temp = new GeoPoint[2];
		temp[0] = new GeoPoint((int)(44.8119502868312*1e6),(int)(-0.590930735873974*1e6));
		temp[1] = new GeoPoint((int)(44.8169513627231*1e6),(int)(-0.585678604870352*1e6));
		liste.add(new SpecificLine("Forum--->Roustaing", null, temp));
		Log.i("Routes", "liste size :"+liste.size());
		return liste;
	}


	public static ArrayList<SpecificLine> getSpecificLinesFromCursor(ArrayList<Line> lines,
			Cursor specificLineCursor){

		ArrayList<SpecificLine> specificLines = new ArrayList<SpecificLine>();

		if(specificLineCursor.getCount() == 0 ) return specificLines;

		//------- specific line table -------

		int nameColumn = specificLineCursor
				.getColumnIndex(TramwayDBConstants.KEY_NAME);

		int idColumn = specificLineCursor.getColumnIndex(BaseColumns._ID);
		int lineIdColumn = specificLineCursor.getColumnIndex(TramwayDBConstants.KEY_LINE_ID);

		//------- link table -------

		//		int specificLineColumn = linkTableCursor.getColumnIndex(TramwayDBConstants.KEY_SPECIFIC_LINE_ID);
		//		int stationColumn = linkTableCursor.getColumnIndex(TramwayDBConstants.KEY_STATION_ID);

		specificLineCursor.moveToFirst();
		SpecificLine l = null;

		int size = specificLineCursor.getCount();

		for(int i =  0; i < size; i ++){
			l = new SpecificLine();
			l.id = specificLineCursor.getInt(idColumn);
			l.name = specificLineCursor.getString(nameColumn);
			int id_ligne = specificLineCursor.getInt(lineIdColumn);


			//On recup la ligne
			for(int j = 0; j < lines.size(); j++)
				if(id_ligne == lines.get(j).id )l.ligne = lines.get(j);

			//-----------------------------------------------
			// Boucle sur la table de linkage
			//			int size2 = specificLineCursor.getCount();
			//			specificLineCursor.moveToFirst();
			//			for(int j = 0; j < size2; j ++){
			//				if(linkTableCursor.getInt(specificLineColumn) == l.id){
			//				}
			//			}

			specificLines.add(l);
			specificLineCursor.moveToNext();


		}

		specificLineCursor.close();

		return specificLines;
	}

}
